/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.infor.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.infor.entity.LnCustomers;
import com.jeeplus.modules.infor.mapper.LnCustomersMapper;
import com.jeeplus.modules.infor.entity.CustomerGoodsContror;
import com.jeeplus.modules.infor.mapper.CustomerGoodsControrMapper;
import com.jeeplus.modules.infor.entity.ReAddress;
import com.jeeplus.modules.infor.mapper.ReAddressMapper;

/**
 * 下游客户Service
 * @author 李超辉
 * @version 2018-04-11
 */
@Service
@Transactional(readOnly = true)
public class LnCustomersService extends CrudService<LnCustomersMapper, LnCustomers> {

	@Autowired
	private CustomerGoodsControrMapper customerGoodsControrMapper;
	@Autowired
	private ReAddressMapper reAddressMapper;
	
	public LnCustomers get(String id) {
		LnCustomers lnCustomers = super.get(id);
		lnCustomers.setCustomerGoodsControrList(customerGoodsControrMapper.findList(new CustomerGoodsContror(lnCustomers)));
		lnCustomers.setReAddressList(reAddressMapper.findList(new ReAddress(lnCustomers)));
		return lnCustomers;
	}
	
	public List<LnCustomers> findList(LnCustomers lnCustomers) {
		return super.findList(lnCustomers);
	}
	
	public Page<LnCustomers> findPage(Page<LnCustomers> page, LnCustomers lnCustomers) {
		return super.findPage(page, lnCustomers);
	}
	
	@Transactional(readOnly = false)
	public void save(LnCustomers lnCustomers) {
		super.save(lnCustomers);
		for (CustomerGoodsContror customerGoodsContror : lnCustomers.getCustomerGoodsControrList()){
			if (customerGoodsContror.getId() == null){
				continue;
			}
			if (CustomerGoodsContror.DEL_FLAG_NORMAL.equals(customerGoodsContror.getDelFlag())){
				if (StringUtils.isBlank(customerGoodsContror.getId())){
					customerGoodsContror.setCustomer(lnCustomers);
					customerGoodsContror.preInsert();
					customerGoodsControrMapper.insert(customerGoodsContror);
				}else{
					customerGoodsContror.preUpdate();
					customerGoodsControrMapper.update(customerGoodsContror);
				}
			}else{
				customerGoodsControrMapper.delete(customerGoodsContror);
			}
		}
		for (ReAddress reAddress : lnCustomers.getReAddressList()){
			if (reAddress.getId() == null){
				continue;
			}
			if (ReAddress.DEL_FLAG_NORMAL.equals(reAddress.getDelFlag())){
				if (StringUtils.isBlank(reAddress.getId())){
					reAddress.setLnCustomers(lnCustomers);
					reAddress.setProjectName(lnCustomers.getProject().getName());
					reAddress.preInsert();
					reAddressMapper.insert(reAddress);
				}else{
					reAddress.preUpdate();
					reAddressMapper.update(reAddress);
				}
			}else{
				reAddressMapper.delete(reAddress);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(LnCustomers lnCustomers) {
		super.delete(lnCustomers);
		customerGoodsControrMapper.delete(new CustomerGoodsContror(lnCustomers));
		reAddressMapper.delete(new ReAddress(lnCustomers));
	}
	
}